# import requests
# import base64
# import json
#
# import os
# import sys
# import logging
# from requests.auth import HTTPBasicAuth
# import uuid
# import hashlib
# import time
#
# # LanguageMulti = {
# #     "ru": 0,
# #     "ha": 1,
# #     "yue": 2,
# #     "wei": 3,
# #     "bo": 9,
# #
# # }
# class head1:
#     def __init__(self, key, timestamp, sign):
#         self.key = key
#         self.timestamp = timestamp
#         self.sign = sign
#
# class OcrImageHw:
#     client_id = "a91-user-fuweiqiang"  # 输入注册后获取ID
#     client_secret = "c7d8961ca5e74168b"  # 输入注册后获取secret
#
#     def connect_new(self, base64_data, language):
#         '''
#
#         key - 公钥，privateKey - 私钥（用户要负责保密好），timestamp – 当前时间戳
#         string sign_str = “key&privateKey&timestamp”
#         string sign = MD5(sign_str)
#         '''
#         key = " "  # 输入注册后获取 ID
#         privateKey = " "  # 输入注册后获取 secret
#         timestamp = time.time()
#         timestamp = str(int(round(timestamp * 1000)))
#         sign_str = key & privateKey & timestamp
#         sign = hashlib.md5(sign_str.encode('utf8')).hexdigest()
#         # header = head1(key,timestamp,sign)
#         header = {'key': key, 'timestamp': timestamp,'sign':sign}
#
#         print('language: ' + language)
#         body = {'img': base64_data, 'language': language}
#
#         r = requests.post('http://61.50.104.126:81/ysweb/ocr/ft-scene/file', headers=header, data=body)
#
#         print("url request Completed!")
#         return r.content
#
#
# file = "D:\\1.png"
# result_dir = "D:\\"
# hwmodel = OcrImageHw()
# language = "bo"
#
#
# def test():
#     imgData = open(r"%s" % file, "rb")
#     base64data = str(base64.b64encode(imgData.read()), encoding="utf-8")
#     response = hwmodel.connect_new(base64data, language)
#     imgData.close()
#     ocrResult2Json = json.loads(response.decode("utf-8"))
#     print(ocrResult2Json)
#     recognise_str = ''
#     real_value_str = ''
#     to_write_file_str = ''
#     if 'Paraph' in ocrResult2Json:
#         for paraph in ocrResult2Json['Paraph']:
#             for line in paraph['line']:
#                 # recognise_str += ' '
#                 to_write_file_str += line['code'] + ' '
#                 recognise_str += line['code'] + ' '
#             to_write_file_str += '\n'
#     elif 'Table' in ocrResult2Json:
#         for table in ocrResult2Json['Table']:
#             for cell in table['cell']:
#                 for paraph in cell['paragh']:
#                     for line in paraph['line']:
#                         to_write_file_str += line['code'] + ' '
#                         recognise_str += line['code'] + ' '
#             to_write_file_str += '\n'
#
#     with open(r"%s" % result_dir + "result.txt", 'wb') as f1:
#         f1.write(to_write_file_str.encode('utf-8'))
#
#
# test()





import requests
import base64
import json

LanguageMulti = {
    "ru" : 0,
    "ha":1,
    "yue":2,
    "wei":3,
    "bo":9,

}


class OcrImageHw:

    def connect_new(self, base64_data, language):

        print('language: ' + language)
        body = {'img': base64_data, 'from': "zh", "to": "en"}
        headers = {
            "Content-Type": "application/x-www-form-urlencoded"
        }
        r = requests.post('http://120.92.70.225:80/hgd/ocr_image_merge', headers=headers, data=body)

        print("url request Completed!")
        return r.content

file = "D:\\zh.png"
result_dir = "D:\\"
hwmodel = OcrImageHw()
language = "bo"

def test():
    imgData = open(r"%s" % file, "rb")
    base64data = str(base64.b64encode(imgData.read()), encoding="utf-8")
    response = hwmodel.connect_new(base64data, language)
    imgData.close()
    ocrResult2Json = json.loads(response.decode("utf-8"))
    print(ocrResult2Json)
    recognise_str = ''
    real_value_str = ''
    to_write_file_str = ''
    if 'Paraph' in ocrResult2Json:
        for paraph in ocrResult2Json['Paraph']:
            for line in paraph['line']:
                # recognise_str += ' '
                to_write_file_str += line['code'] + ' '
                recognise_str += line['code'] + ' '
            to_write_file_str += '\n'
    elif 'Table' in ocrResult2Json:
        for table in ocrResult2Json['Table']:
            for cell in table['cell']:
                for paraph in cell['paragh']:
                    for line in paraph['line']:
                        to_write_file_str += line['code'] + ' '
                        recognise_str += line['code'] + ' '
            to_write_file_str += '\n'

    with open(r"%s" % result_dir + "result.txt", 'wb') as f1:
        f1.write(to_write_file_str.encode('utf-8'))


test()
